// pages/LotteryPersonInfo/LotteryPersonInfo.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        person: {
            id: null,
            name: "",
            applyNum: "",
            openId: null,
        },
        isEdit: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log("onLoad,options:", options);
        var openId = "person.openId";
        var token = wx.getStorageSync("token");
        this.setData({
            [openId]: token
        });

        var id = options.id;
        console.log("id:", id);
        if (id && typeof(id) != "undefined" && id != '') {
            this.getPersonById(id);
        }
    },

    nameOnChange(e) {
        var name = 'person.name';
        this.setData({
            [name]: e.detail.value,
            isEdit: true
        })
    },
    applyNumOnChange(e) {
        var applyNum = 'person.applyNum';
        this.setData({
            [applyNum]: e.detail.value,
            isEdit: true
        })
    },

    saveOrUpdateLotteryPerson: function() {
        var id = this.data.person.id;
        console.log("id:", id);
        if (id == null) {
            //新增
            this.savePerson();
        } else {
            //修改
            this.updatePerson();
        }
        this.setData({
            "isEdit":false
        })
    },

    getPersonById: function(id) {
        wx.showLoading({
            title: '数据加载中',
        });
        //设置默认值
        var that = this;
        var postData = {
            id: id
        };
        var url = util.baseUrl + "/car/getPersonById";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function(res) {
                wx.hideLoading();
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.msg,
                        showCancel: false,
                        confirmText: '确定'
                    });
                } else {
                    that.setData({
                        person: res.data
                    });
                    console.log(that.data);
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
            }
        })
    },

    savePerson: function() {
        console.log("新增摇号人");
        //设置默认值
        var that = this;
        var postData = this.data.person;
        var url = util.baseUrl + "/car/saveLotteryPerson";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '提示',
                        content: res.msg,
                        showCancel: false,
                        confirmText: '确定'
                    });
                } else {
                    var id = "person.id";
                    that.setData({
                        [id]: res.data.id
                    });
                    wx.showToast({
                        title: '操作成功',
                    })
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
            }
        })
    },

    updatePerson: function() {
        console.log("修改摇号人");
        //设置默认值
        var that = this;
        var postData = this.data.person;
        var url = util.baseUrl + "/car/updateLotteryPerson";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'POST',
            header: {
                'content-type': 'application/json',
            },
            success: function(res) {
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '提示',
                        content: res.msg,
                        showCancel: false,
                        confirmText: '确定'
                    });
                } else {
                    wx.showToast({
                        title: '操作成功'
                    });
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
            }
        })
    },
    goBack: function() {
        wx.navigateBack();
    },
    deletePerson: function() {
        console.log("删除摇号人");
        //设置默认值
        var that = this;
        var postData = {
            id: this.data.person.id
        };
        var url = util.baseUrl + "/car/deleteLotteryPerson";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'GET',
            header: {
                // 'content-type': 'application/json',
            },
            success: function(res) {
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.msg,
                        showCancel: false,
                        confirmText: '确定'
                    });
                } else {
                    wx.showToast({
                        title: '操作成功'
                    });
                    wx.navigateBack();
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
            }
        })
    },
    queryIndexByPersonId: function() {
        console.log("根据摇号编码查询指标");

        if(this.data.isEdit){
            wx.showModal({
                title: '温馨提示',
                content: '请先点击保存,再查询',
                showCancel:false
            });
            return;
        }

        //设置默认值
        var that = this;
        var postData = {
            id: this.data.person.id
        };
        var url = util.baseUrl + "/car/queryIndexByPersonId";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'GET',
            header: {
                // 'content-type': 'application/json',
            },
            success: function(res) {
                //处理响应数据
                that.parseRes(res.data);
            },
            fail: function(e) {
                console.log("请求失败:", e);
            }
        })
    },

    parseRes: function(res) {
        console.log("响应:", res);
        if (res.code != 1) {
            //显示msg
            console.log("请求出错,错误原因:", res.msg);
            wx.showModal({
                title: '温馨提示',
                content: res.msg,
                showCancel: false
            });
        } else {
            //判断数组是否为空
            if (res.data.length == 0) {
                console.log("数组为空");
                wx.navigateTo({
                    url: '../carQueryFail/carQueryFail'
                })
            } else if (res.data.length == 1) {
                console.log("data:", res.data);
                let indexList = JSON.stringify(res.data);
                wx.navigateTo({
                    url: '../carQuerySuccessOnly/carQuerySuccessOnly?indexList=' + escape(indexList),
                })
            } else {
                console.log("data:", res.data);
                let indexList = JSON.stringify(res.data);
                wx.navigateTo({
                    url: '../carQuerySuccess/carQuerySuccess?indexList=' + escape(indexList),
                })
            }
        }
    },
})